﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace MicroBlogPost.Libs
{
    class MicroBlogSina
    {
        private string userName;
        private string Password;
        private oAuthSina oauth = new oAuthSina();
        string basic = "http://api.t.sina.com.cn/";

        public MicroBlogSina()
        {

        }
        public bool Login(string username, string password)
        {
            userName = username;
            Password = password;
            oauth.Login(username, password);
            oauth.RequestTokenGet();
            oauth.AuthorizationGet();
            oauth.Verifier = "xinshangbo";
            oauth.AccessTokenGet();
            return true;
        }
        public bool Post(string content)
        {

            string url = basic + "statuses/update.xml";
            string retMsg = oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + content);
            return true;    
        }
        public bool Reply(string id, string content)
        {
            string retMsg;
            string url = basic + "users/show.xml";
            retMsg = oauth.oAuthWebRequest(oAuthSina.Method.GET, url, "user_id=" + id);

            XmlDocument dom = new XmlDocument();
            dom.LoadXml(retMsg);
            XmlNode nodeText=dom.SelectSingleNode("//user//status//id");
            string msgId = nodeText.InnerText;

            url = basic + "statuses/comment.xml";
            retMsg = oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "id=" + msgId+"&comment="+content);

            return true;
        }
        public bool Post2Group(string groupid, string content)
        {
            Reply(groupid, content);
            return true;
        }
        public void At2People(string username, string content)
        {
            Post("@" + username + " " + content);
        }
     }
}
